// 需要放在bossEventTurnCount后。
// 会刷掉 Game_Troop.prototype.meetsBattleEventConditionsEnemyHp
// 和 Game_Troop.prototype.meetsBattleEventConditionsActorHp

let SWDA_Game_Troop_setup = Game_Troop.prototype.setup;
Game_Troop.prototype.setup = function(troopId){
	SWDA_Game_Troop_setup.call(this, troopId);
	this.setupBattlerDeadFlag();	//与事件回合无关，与敌人血量（死亡）条件相关
};

// 如果战斗事件存在标签：<Actor Hp Event Need Dead>或者<Enemy Hp Event Need Dead>
// 则角色/敌人死亡时满足条件，触发事件。
Game_Troop.prototype.setupBattlerDeadFlag = function(){
    var pages = this.troop().pages;
    var reg = /<([\s\S]+) Hp Event Need Dead>/;
    for(var i = 0; i < pages.length; i++){
        var page = pages[i];
        page.hpEventNeedDead = null;

        var list = page.list;
        list.forEach(function(ev){
            if([108, 408].contains(ev.code)){
                if(reg.exec(ev.parameters[0]))
                    page.hpEventNeedDead = RegExp.$1.trim().toUpperCase();
            }
        });
    }
};

// 饕：如果事件页含<Enemy Hp Event Need Dead>，则在角色死亡后触发事件。
Game_Troop.prototype.meetsBattleEventConditionsEnemyHp = function(page){
	
    var condition = page.conditions;
    var needDead = page.hpEventNeedDead == "ENEMY";
    var enemy = $gameTroop.members()[condition.enemyIndex];
    if(!enemy)
        return false;
    if(needDead && condition.enemyHp <= 0 && !enemy.isDead())
        return false;
    if(condition.enemyHp > 0 && enemy.hp <= 0)
        return false;
    if(enemy.hpRate() * 100 > condition.enemyHp)
        return false;
    return true;

   /*
    var enemy = $gameTroop.members()[c.enemyIndex];
    if (!enemy || enemy.hpRate() * 100 > c.enemyHp) {
        return false;
    }
    */
};

// 饕：如果事件页含<Actor Hp Event Need Dead>，则在角色死亡后触发事件。
Game_Troop.prototype.meetsBattleEventConditionsActorHp = function(page){
    var condition = page.conditions;
    var needDead = page.hpEventNeedDead == "ACTOR";
    var actor = $gameActors.actor(condition.actorId);
    if(!actor)
        return false;
    if(needDead && condition.actorHp <= 0 && !actor.isDead())
        return false;
    if(condition.actorHp > 0 && actor.hp <= 0)
        return false;
    if(actor.hpRate() * 100 > condition.actorHp)
        return false;
    return true;

   /*
    var actor = $gameActors.actor(c.actorId);
    if (!actor || actor.hpRate() * 100 > c.actorHp) {
        return false;
    }
    */
};